package edu.cmu.javadb;

import java.util.*;

//TODO I'm not quit sure the type 
public class VTable<K, T> extends HashMap<K, Collection<T> >  {
	private static final long serialVersionUID = 262617756077031394L;

	public <S> Map<K, S> reduce(ReduceFn<S, T> fn) {
		Map<K, S> res = new HashMap<K, S>();
		
		for (Map.Entry<K, Collection<T> > entry : this.entrySet()) {
			S val = fn.process(entry.getValue());
			res.put(entry.getKey(), val);
		}
		return res;
	}
	
	public void addValue(K key, T val) {
		Collection<T> set;
		if (!containsKey(key)) {
			set = new QueryableSet<T>(); 
			this.put(key, set);
		} else {
			set = this.get(key);
		}
		set.add(val);
	}
}
